var express = require('express');
var router = express.Router();
var auth = require('../middlewares/auth');

var multer = require('multer');
var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, 'public/uploads');
    },
    filename: function(req, file, cb) {
        var ext = file.originalname.slice(file.originalname.lastIndexOf('.'), file.originalname.length);
        cb(null, new Date().getTime() + ext);
    }
});

var upload = multer({
    storage: storage
})
var fs = require('fs');

var position = require('../controller/position');
var worker = require('../controller/worker');
var serie = require('../controller/serie');
var member = require('../controller/member');
var manager = require('../controller/manager');
var sign = require('../controller/sign');


//# 后台首页
//
router.get('/', auth.managerRequired, function(req, res) {
    res.render('backend/index', {
        title: "首页"
    });
});

//# 登录
//
router.get('/login', sign.login);
router.post('/login', sign.createSession);
router.get('/logout', sign.logout);


//# 职位管理
//
router.get('/positions', auth.managerRequired, position.index);
router.get('/positions/list', auth.managerRequired, position.list);
router.get('/position/new', auth.managerRequired, position.new);
router.post('/position/create', auth.managerRequired, position.create);
router.get('/position/:id', auth.managerRequired, position.edit);
router.put('/position/:id', auth.managerRequired, position.update);
router.delete('/position/:id', auth.managerRequired, position.delete);

//# 员工管理
//
router.get('/workers', auth.managerRequired, worker.index);
router.get('/workers/list', auth.managerRequired, worker.list);
router.get('/worker/new', auth.managerRequired, worker.new);
router.post('/worker/create', auth.managerRequired, worker.create);
router.get('/worker/:id', auth.managerRequired, worker.edit);
router.put('/worker/:id', auth.managerRequired, worker.update);
router.delete('/worker/:id', auth.managerRequired, worker.delete);

//# 套系管理
//
router.get('/series', auth.managerRequired, serie.index);
router.get('/series/list', auth.managerRequired, serie.list);
router.get('/serie/new', auth.managerRequired, serie.new);
router.post('/serie/create', auth.managerRequired, serie.create);
router.get('/serie/:id', auth.managerRequired, serie.edit);
router.put('/serie/:id', auth.managerRequired, serie.update);
router.delete('/serie/:id', auth.managerRequired, serie.delete);
router.post('/serie/picture', upload.array('files'), auth.managerRequired, serie.pictureManage);


//# 客户管理
//
router.get('/members', auth.managerRequired, member.index);
router.get('/members/list', auth.managerRequired, member.list);
router.get('/member/new', auth.managerRequired, member.new);
router.post('/member/create', auth.managerRequired, member.create);
router.get('/member/:id', auth.managerRequired, member.edit);
router.put('/member/:id', auth.managerRequired, member.update);
router.delete('/member/:id', auth.managerRequired, member.delete);


//# 管理员管理
//
router.get('/managers', auth.managerRequired, manager.index);
router.get('/managers/list', auth.managerRequired, manager.list);
router.get('/manager/new', auth.managerRequired, manager.new);
router.post('/manager/create', auth.managerRequired, manager.create);
router.delete('/manager/:id', auth.managerRequired, manager.delete);
router.get('/util/manager_init', manager.init);

module.exports = router;